using System;
using System.ComponentModel;
using System.Windows.Forms;
using BizTalk.BuildGenerator.UI.Navigation;
using BizTalk.BuildGenerator.UI.UserControls;

namespace BizTalk.BuildGenerator.UI
{
    /// <summary>
    /// The form hosting the wizard
    /// </summary>
    public partial class WizardForm : Form
    {
        private GenerationArgs _Args = new GenerationArgs();

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        public WizardForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="args"></param>
        public WizardForm(GenerationArgs args)
        {
            InitializeComponent();
            if (!DesignMode)
            {
                _Args = args;
            }
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        public void GenerationComplete()
        {
            BuildFileGenerationWizard.SelectedPage = BuildFileGenerationWizard.SelectedPage.NextPage;
        }

        /// <summary>
        /// Cancels the wizard
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void wizard1_Cancel(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Wizard load form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WizardForm_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                BuildFileGenerationWizard.SelectedPage = IntroductionPage;
                RegisterArgsWithControls();
                SetupWizardSteps();
                generateFilesControl1.GenerationComplete +=
                    new GenerateFilesControl.GenerationCompleteDelegate(GenerationComplete);
            }
        }

        #region Private Methods

        /// <summary>        
        /// </summary>
        private void SetupWizardSteps()
        {
            //Next steps
            this.IntroductionPage.NextPage = this.ChooseApplicationPage;
            this.ChooseApplicationPage.NextPage = this.BuildPropertiesWizardPage;
            this.BuildPropertiesWizardPage.NextPage = this.BuildReferencesPage;
            this.BuildReferencesPage.NextPage = this.WebServicePublishingPage;
            this.WebServicePublishingPage.NextPage = this.bizTalkHostsPage;
            this.bizTalkHostsPage.NextPage = this.ApplicationBindingsPage;
            this.ApplicationBindingsPage.NextPage = this.ApplicationDeploymentPage;
            this.ApplicationDeploymentPage.NextPage = this.UnitTestingPage;
            this.UnitTestingPage.NextPage = this.GenerationOptionsWizardPage;
            this.GenerationOptionsWizardPage.NextPage = this.GenerateFilesPage;
            this.GenerateFilesPage.NextPage = this.FinalPage;

            //Previous steps
            this.IntroductionPage.PreviousPage = null;
            this.ChooseApplicationPage.PreviousPage = this.IntroductionPage;
            this.BuildPropertiesWizardPage.PreviousPage = this.ChooseApplicationPage;
            this.BuildReferencesPage.PreviousPage = this.BuildPropertiesWizardPage;
            this.WebServicePublishingPage.PreviousPage = this.BuildReferencesPage;
            this.bizTalkHostsPage.PreviousPage = this.WebServicePublishingPage;
            this.ApplicationBindingsPage.PreviousPage = this.bizTalkHostsPage;
            this.ApplicationDeploymentPage.PreviousPage = this.ApplicationBindingsPage;
            this.UnitTestingPage.PreviousPage = this.ApplicationDeploymentPage;
            this.GenerationOptionsWizardPage.PreviousPage = this.UnitTestingPage;
            this.GenerateFilesPage.PreviousPage = this.GenerationOptionsWizardPage;
            this.FinalPage.PreviousPage = this.GenerateFilesPage;
        }
        /// <summary>
        /// Registers the appropriate data capture object with its control
        /// </summary>
        private void RegisterArgsWithControls()
        {
            referencesControl1.RegisterReferences(_Args.BuildReferences);
            applicationBindingsControl1.RegisterBindings(_Args.ApplicationBindings);
            applicationDeploymentControl1.RegisterDeployment(_Args.ApplicationDeployment);
            applicationDescriptionControl1.RegisterArgs(_Args);
            publishWebServicesControl1.RegisterWebServices(_Args);
            unitTestingControl.RegisterUnitTesting(_Args.UnitTesting);
            generateFilesControl1.RegisterArgs(_Args);
            bizTalkHostsControl1.RegisterHosts(_Args);
            generationOptionsControl1.RegisterArgs(this._Args);
            buildPropertiesControl1.RegisterBuildProperties(this._Args.BuildProperties);
        }

        #endregion

        #region Wizard Before Display Events

        private void ApplicationDeploymentPage_BeforeDisplay(object sender, EventArgs e)
        {
        }

        private void UnitTestingPage_BeforeDisplay(object sender, EventArgs e)
        {
        }

        private void FinalPage_BeforeDisplay(object sender, EventArgs e)
        {
        }

        #endregion

        #region Wizard Before Move Next Handlers

        private void ChooseApplicationPage_BeforeMoveNext(object sender, CancelEventArgs e)
        {
            MoveNextResult result = applicationDescriptionControl1.MoveNext();
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        private void BuildReferencesPage_BeforeMoveNext(object sender, CancelEventArgs e)
        {
            MoveNextResult result = referencesControl1.MoveNext();
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        private void WebServicePublishingPage_BeforeMoveNext(object sender, CancelEventArgs e)
        {
            MoveNextResult result = publishWebServicesControl1.MoveNext();
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        private void ApplicationBindingsPage_BeforeMoveNext(object sender, CancelEventArgs e)
        {
            MoveNextResult result = applicationBindingsControl1.MoveNext();
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        private void ApplicationDeploymentPage_BeforeMoveNext(object sender, CancelEventArgs e)
        {
            MoveNextResult result = applicationDeploymentControl1.MoveNext();
            if (result.ContainsError)
                MessageBox.Show(result.ErrorMessage);
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        private void UnitTestingPage_BeforeMoveNext(object sender, CancelEventArgs e)
        {
            MoveNextResult result = unitTestingControl.MoveNext();
            if (result.ContainsError)
                MessageBox.Show(result.ErrorMessage);
            if (!result.CanMoveNext)
                e.Cancel = true;
        }

        /// <summary>
        /// Before generated files moves next
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GenerateFilesPage_BeforeMoveNext(object sender, CancelEventArgs e)
        {
            MoveNextResult result = generateFilesControl1.MoveNext();
            if (!result.CanMoveNext)
                e.Cancel = true;

            GenerateFilesPage.AllowMoveNext = false;
        }

        private void FinalPage_BeforeMoveNext(object sender, CancelEventArgs e)
        {
            DialogResult dr =
                MessageBox.Show("Would you like to save the build so it can be used again?", "Save",
                                MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                dr = saveArgs.ShowDialog();
                if (dr == DialogResult.OK)
                    ArgsHelper.Save(saveArgs.FileName, _Args);
            }
            Close();
        }
        private void GenerationOptionsWizardPage_BeforeMoveNext(object sender, CancelEventArgs e)
        {
            MoveNextResult result = generationOptionsControl1.MoveNext();
            if (result.ContainsError)
                MessageBox.Show(result.ErrorMessage);
            if (!result.CanMoveNext)
                e.Cancel = true;
        }
        private void BuildPropertiesWizardPage_BeforeMoveNext(object sender, CancelEventArgs e)
        {
            MoveNextResult result = buildPropertiesControl1.MoveNext();
            if (result.ContainsError)
                MessageBox.Show(result.ErrorMessage);
            if (!result.CanMoveNext)
                e.Cancel = true;
        }
        #endregion

        

        
    }
}